package com.offcn.gateway.resolver;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * 限流
 */

@Configuration
public class MyIpKeyResolver {

    @Bean(name = "ipKeyResolver")
    public KeyResolver userKeyRevolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                // 通过exchange可以获取 请求对象 和 响应对象
                String ip = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
                System.out.println("客户端IP:" + ip);
                return Mono.just(ip);
            }
        };
    }

}
